<template>
	<scroll-view>
		<view class="cu-bar bg-white">
			<view class="action">
				<text class="cuIcon-title text-blue"></text> 资源列表
			</view>
		</view>
		<view class="cu-list menu">
			<view class="cu-item" v-for="(item, index) in resInfo" :key="index">
				<view class="content" @click="openRes(index)">
					<view class="text-left text-grey">{{item.RES_NAME_}}</view>
				</view>
				<view class="action">
					<button class="cu-btn light bg-red cuIcon" @tap="delRes(index)">
						<text class="cuIcon-delete"></text>
					</button>
					<button class="cu-btn light bg-blue cuIcon margin-left-xs" open-type="share" :id="index">
						<text class="cuIcon-share"></text>
					</button>
				</view>
			</view>
		</view>
		<view class="add-click">
			<button class="lg cu-btn light bg-blue cuIcon" @tap="openAdd">
				<text class="lg cuIcon-roundaddfill"></text>
			</button>
		</view>
	</scroll-view>
</template>

<script>
	export default {
		data() {
			return {
				modalName: '',
				resInfo: [],
				shareLineMsg: [{
					title: '共享好东西',
					path: '/pages/index/index',
					imageUrl: '/static/share.png'
				}, {
					title: '越分享，越有爱',
					path: '/pages/index/index',
					imageUrl: '/static/share.png'
				}, {
					title: '好东西，要共享',
					path: '/pages/index/index',
					imageUrl: '/static/share.png'
				}]
			}
		},
		methods: {
			openAdd(){
				uni.navigateTo({
					url: '/pages/new/new'
				});
			},
			delRes(index){
				let that = this;
				let resData = that.resInfo[index]
				uni.request({
					url: that.baseUrl + "/delete",
					data: {
						resId: resData.UUID_
					},
					success(res) {
						if (res.statusCode == 200) {
							that.list();
						} else {
							uni.showToast({
								icon: 'none',
								title: '删除资源失败，请重试！',
								duration: 1200
							});
						}
					},
					fail(res) {
						uni.showToast({
							icon: 'none',
							title: '服务器开了小差，请稍后再试！',
							duration: 1200
						});
					}
				});
			},
			openRes(index){
				let that = this;
				uni.navigateTo({
					url: '../new/new?id=' + that.resInfo[index].UUID_
				});
			},
			list(){
				let that = this;
				uni.request({
					url: that.baseUrl + "/resList",
					data: {
						userId: uni.getStorageSync('mini_sfshare_key')
					},
					success(res) {
						if (res.statusCode == 200) {
							that.resInfo = res.data;
						} else {
							uni.showToast({
								icon: 'none',
								title: '获取消息失败，请重试！',
								duration: 1200
							});
						}
					},
					fail(res) {
						uni.showToast({
							icon: 'none',
							title: '服务器开了小差，请稍后再试！',
							duration: 1200
						});
					}
				});
			}
		},
		onShareAppMessage(e) {
			let res = this.resInfo[e.target.id]
			return {
				title: '分享了《' + res.RES_NAME_ + '》文件, 点击查看',
				path: '/pages/share/share?id=' + res.UUID_,
				imageUrl: '/static/share.png'
			};
		},
		onShareTimeline() {
			return this.shareLineMsg[Math.floor(Math.random() * this.shareMsg.length)];
		},
		onShow() {
			this.list()
		},
		onPullDownRefresh() {
			this.list();
			uni.stopPullDownRefresh();
		}
	}
</script>

<style>
	.add-click{
	  border-radius: 100%;
	  position: fixed;
	  bottom: 120rpx;
	  right: 50rpx;
	  display: flex;
	  align-items: center;
	  justify-content: center;
	  z-index: 9;
	}
</style>
